package com.qyhl.webtv.module_user.shop.code;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.user.ShoppingOrderBean;
import com.qyhl.webtv.module_user.R;
import com.qyhl.webtv.module_user.R2;
import com.qyhl.webtv.module_user.share.UserShareUtil;
import com.weavey.loading.lib.LoadingLayout;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.SHOP_CODE)
public class ShopCodeActivity extends BaseActivity implements ShopCodeContract.ShopCodeView {
    @BindView(R2.id.status)
    TextView mStatus;
    @BindView(R2.id.title)
    TextView titleName;
    @BindView(R2.id.price)
    TextView price;
    @BindView(R2.id.deadline)
    TextView deadline;
    @BindView(R2.id.qr_code)
    ImageView qrCode;
    @BindView(R2.id.code_num)
    TextView codeNum;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private String itemId;

    private String deadLine;

    private String startTime;

    private ShopCodePresenter mPresenter;

    @Override
    protected int getContentView() {
        return R.layout.login_activity_shop_code;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);
        mPresenter = new ShopCodePresenter(this);
        itemId = getIntent().getStringExtra("id");
        deadLine = getIntent().getStringExtra("deadline");
        startTime = getIntent().getStringExtra("start");
        mPresenter.getDetail(itemId);
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getDetail(itemId);
            }
        });
    }


    @OnClick(R2.id.back_btn)
    public void onViewClicked() {
        finish();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void setDetail(ShoppingOrderBean bean) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Success);

        titleName.setText(bean.getItemName());

        if (deadLine.equals("不限期")) {
            deadline.setText("有效期：" + DateUtils.translateToShopping(deadLine));
        } else {
            if (StringUtils.isEmpty(startTime)) {
                deadline.setText("有效期：" + DateUtils.translateToShopping(deadLine));
            } else {
                deadline.setText("有效期：" + DateUtils.toShoppingStartTime(startTime) + " ~ " + DateUtils.translateToShopping(deadLine));
            }
        }

        price.setText("总价：" + bean.getItemPrice() + "积分");

        codeNum.setText(bean.getId());

        if (bean.getOrderStatus().equals("NOT_CHECKED")) {
            if (deadLine.equals("不限期") || DateUtils.isBeforeTime(deadLine)) {
                mStatus.setText("待兑换");
                codeNum.setTextColor(getResources().getColor(R.color.global_black_lv2));
                UserShareUtil shareUtil = new UserShareUtil(this, 200, bean.getId(), qrCode);
            } else {
                mStatus.setText("已过期");
                codeNum.setTextColor(getResources().getColor(R.color.shop_41per_mask));
                UserShareUtil shareUtil = new UserShareUtil(this, 200, bean.getId(), qrCode, true);
            }
        } else {
            mStatus.setText("已兑换");
            codeNum.setTextColor(getResources().getColor(R.color.shop_41per_mask));
            UserShareUtil shareUtil = new UserShareUtil(this, 200, bean.getId(), qrCode, true);
        }

    }

    @Override
    public void setError(String msg) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Error);
        if (!NetUtil.isNetworkAvalible(this)) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }
}
